package com.example.androidtraining_ahut2020;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivityDay10Calculator extends Activity implements OnClickListener {
	private EditText editText;
	private TextView screen;
	private Button del;
	private Button res;
	private Button div;
	private Button mul;
	private Button seven;
	private Button eight;
	private Button nine;
	private Button sub;
	private Button four;
	private Button five;
	private Button six;
	private Button add;
	private Button one;
	private Button two;
	private Button three;
	private Button zero;
	private Button dot;
	private Button equ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_day10_calc);
		initView();
		initEditText();
	}

	private void initView() {
		editText = (EditText) findViewById(R.id.input);
		screen = (TextView) findViewById(R.id.answer);
		del = (Button) findViewById(R.id.del);
		res = (Button) findViewById(R.id.res);
		div = (Button) findViewById(R.id.div);
		mul = (Button) findViewById(R.id.mul);
		seven = (Button) findViewById(R.id.seven);
		eight = (Button) findViewById(R.id.eight);
		nine = (Button) findViewById(R.id.nine);
		sub = (Button) findViewById(R.id.sub);
		four = (Button) findViewById(R.id.four);
		five = (Button) findViewById(R.id.five);
		six = (Button) findViewById(R.id.six);
		add = (Button) findViewById(R.id.add);
		one = (Button) findViewById(R.id.one);
		two = (Button) findViewById(R.id.two);
		three = (Button) findViewById(R.id.three);
		zero = (Button) findViewById(R.id.zero);
		dot = (Button) findViewById(R.id.dot);
		equ = (Button) findViewById(R.id.equ);
		del.setOnClickListener(this);
		/**
		 * 长按的效果
		 */
		del.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View arg0) {
				editText.setText("0");
				screen.setText("");
				return false;
			}
		});
		res.setOnClickListener(this);
		div.setOnClickListener(this);
		mul.setOnClickListener(this);
		seven.setOnClickListener(this);
		eight.setOnClickListener(this);
		nine.setOnClickListener(this);
		sub.setOnClickListener(this);
		four.setOnClickListener(this);
		five.setOnClickListener(this);
		six.setOnClickListener(this);
		add.setOnClickListener(this);
		one.setOnClickListener(this);
		two.setOnClickListener(this);
		three.setOnClickListener(this);
		zero.setOnClickListener(this);
		dot.setOnClickListener(this);
		equ.setOnClickListener(this);
	}

	private void initEditText() {
		editText.setTextAppearance(getApplicationContext(), R.style.DarkText);
		editText.setText("0");
	}

	int p = 1;

	/**
	 * 正常按下的效果
	 */
	@Override
	public void onClick(View v) {
		if (v == del) {
			p = 1;
			List<String> list1 = new ArrayList<String>();
			String s1 = editText.getText().toString();
			int t = s1.length();
			for (int i = 0; i < s1.length(); i++) {
				list1.add(s1.charAt(i) + "");
			}
			list1.remove(t - 1);
			String s2 = new String();
			for (int i = 0; i < list1.size(); i++) {
				s2 += list1.get(i);
			}
			if (s2.equals("")) {
				editText.setText("0");
			} else
				editText.setText(s2);
		} else if (v == res) {
			editText.setText(screen.getText());
		} else if (v == div) {
			if (p == 1) {
				editText.setText("");
				p++;
			}
			String str1 = editText.getText().toString();
			editText.setText(str1 + "÷");
			editText.setSelection(str1.length() + 1);
		} else if (v == mul) {
			if (p == 1) {
				editText.setText("");
				p++;
			}
			String str1 = editText.getText().toString();
			editText.setText(str1 + "×");
			editText.setSelection(str1.length() + 1);
		} else if (v == seven) {
			if (p == 1) {
				editText.setText("");
				p++;
			}
			String str1 = editText.getText().toString();
			editText.setText(str1 + "7");
			editText.setSelection(str1.length() + 1);
		} else if (v == eight) {
			if (p == 1) {
				editText.setText("");
				p++;
			}
			String str1 = editText.getText().toString();
			editText.setText(str1 + "8");
			editText.setSelection(str1.length() + 1);
		} else if (v == nine) {
			if (p == 1) {
				editText.setText("");
				p++;
			}
			String str1 = editText.getText().toString();
			editText.setText(str1 + "9");
			editText.setSelection(str1.length() + 1);
		} else if (v == sub) {
			if (p == 1) {
				editText.setText("");
				p++;
			}
			String str1 = editText.getText().toString();
			editText.setText(str1 + "-");
			editText.setSelection(str1.length() + 1);
		} else if (v == four) {
			if (p == 1) {
				editText.setText("");
				p++;
			}
			String str1 = editText.getText().toString();
			editText.setText(str1 + "4");
			editText.setSelection(str1.length() + 1);
		} else if (v == five) {
			if (p == 1) {
				editText.setText("");
				p++;
			}
			String str1 = editText.getText().toString();
			editText.setText(str1 + "5");
			editText.setSelection(str1.length() + 1);
		} else if (v == six) {
			if (p == 1) {
				editText.setText("");
				p++;
			}
			String str1 = editText.getText().toString();
			editText.setText(str1 + "6");
			editText.setSelection(str1.length() + 1);
		} else if (v == add) {
			if (p == 1) {
				editText.setText("");
				p++;
			}
			String str1 = editText.getText().toString();
			editText.setText(str1 + "+");
			editText.setSelection(str1.length() + 1);
		} else if (v == one) {
			if (p == 1) {
				editText.setText("");
				p++;
			}
			String str1 = editText.getText().toString();
			editText.setText(str1 + "1");
			editText.setSelection(str1.length() + 1);
		} else if (v == two) {
			if (p == 1) {
				editText.setText("");
				p++;
			}
			String str1 = editText.getText().toString();
			editText.setText(str1 + "2");
			editText.setSelection(str1.length() + 1);
		} else if (v == three) {
			if (p == 1) {
				editText.setText("");
				p++;
			}
			String str1 = editText.getText().toString();
			editText.setText(str1 + "3");
			editText.setSelection(str1.length() + 1);
		} else if (v == zero) {
			if (p == 1) {
				editText.setText("");
				p++;
			}
			String str1 = editText.getText().toString();
			editText.setText(str1 + "0");
			editText.setSelection(str1.length() + 1);
		} else if (v == dot) {
			if (p == 1) {
				editText.setText("");
				p++;
			}
			String str1 = editText.getText().toString();
			editText.setText(str1 + ".");
			editText.setSelection(str1.length() + 1);
		} else if (v == equ) {
			ArrayList<String> list = new ArrayList<String>();
			String str = new String();

			int k = 0;
			Judge judge = new Judge();
			str = editText.getText().toString();
			int t = judge.symbolNums(str);
			int res = 0;
			for (int i = 0; i < str.length(); i++) {
				if (judge.symbol(str.charAt(i))) {
					list.add(str.substring(k, i));
					k = i + 1;
					res++;
				}
				if (res == t) {
					list.add(str.substring(k, str.length()));
					break;
				}
			}

			/**
			 * 如果按钮是除号或者是点的处理方法
			 */
			if (judge.symbolDiv(str) || judge.symbolPoint(str)) {
				int j = 0;
				double a = Double.valueOf(list.get(j));
				BigDecimal result = new BigDecimal(Double.toString(a));

				j++;
				for (int i = 0; i < str.length(); i++) {
					if (str.charAt(i) == '+') {
						double a1 = Double.valueOf(list.get(j));
						BigDecimal cur = new BigDecimal(Double.toString(a1));
						result = result.add(cur);
						j++;
					} else if (str.charAt(i) == '-') {
						BigDecimal cur = new BigDecimal(list.get(j));
						result = result.subtract(cur);
						j++;
					} else if (str.charAt(i) == '×') {
						BigDecimal shu3 = new BigDecimal(list.get(j));
						result = result.multiply(shu3);
						j++;
					} else if (str.charAt(i) == '÷') {
						double a4 = Double.valueOf(list.get(j));
						BigDecimal shu4 = new BigDecimal(Double.toString(a4));
						result = result.divide(shu4, 5,
								BigDecimal.ROUND_HALF_UP);
						j++;
					}
				}
				screen.setText(result.toString());

			} else {
				int j = 0, s = 0;
				BigInteger shu = new BigInteger(list.get(j));
				BigInteger res1 = BigInteger.ZERO;
				res1 = res1.add(shu);
				j++;
				for (int i = 0; i < str.length(); i++) {
					if (str.charAt(i) == '+') {
						BigInteger shu1 = new BigInteger(list.get(j));
						res1 = res1.add(shu1);
						j++;
					} else if (str.charAt(i) == '-') {
						BigInteger shu2 = new BigInteger(list.get(j));
						res1 = res1.subtract(shu2);
						j++;
					} else if (str.charAt(i) == '×') {
						BigInteger shu3 = new BigInteger(list.get(j));
						res1 = res1.multiply(shu3);
						j++;
					}
				}
				screen.setText(res1 + "");
			}
		}

	}

	/**
	 * 判断符号的类型和计算符号的数量
	 *
	 * @author Administrator
	 */
	class Judge {
		boolean symbolPoint(String string) {
			for (int i = 0; i < string.length(); i++) {
				if (string.charAt(i) == '.') {
					return true;
				}
			}
			return false;
		}

		boolean symbolDiv(String s) {
			for (int i = 0; i < s.length(); i++) {
				if (s.charAt(i) == '÷') {
					return true;
				}
			}
			return false;
		}

		boolean symbol(char a) {
			if (a == '+' || a == '-' || a == '×' || a == '÷') {
				return true;
			}
			return false;
		}

		int symbolNums(String str) {
			int s = 0;
			Judge judge = new Judge();
			for (int i = 0; i < str.length(); i++) {
				if (judge.symbol(str.charAt(i))) {
					s++;
				}
			}
			return s;
		}

	}

}
